package cn.est.test;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            /**
             * 第一个参数Subject：商品名称
             * 第二个参数outTradeNo：编号 - 使用支付编号，业务比较简单，也可以直接使用订单编号
             * 第三个参数totalAmount：支付的总金额
             * 第四个参数resultUrl: 用户成功支付之后，跳转的页面
             */
            AlipayTradePagePayResponse response = Payment.Page()
                    .pay("Apple iPhone11 128G", "2234567890", "5799.00","");
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + response.body);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https";// 协议
        config.gatewayHost = "openapi.alipay.com";// 网关地址
        config.signType = "RSA2";

        config.appId = "2021000118603745";

        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        // 注意这个私钥，是自己的私钥
        config.merchantPrivateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC58ToPbdPw6ZSyEzlhAiCgOrhp5IO0WsIiG+lN6geniIPATRBIcDhtQNhG1QwVEbxV57E8TJRgeeYl7xyQTGQ4lNFhaVe2Wjh4cgmBlYRuNr67B4uxTvRyMqulcp6q8xfR+1Y9aluj5/70+e3sAKevoIJtawslRkZk3RukV6wX5ehvJv6lmXskx472q607z98An5eC2ucymjeDR4hEJcfTXKy3bgIGcXvBqYFO/Mo2ijrTLfpfc3qRgLLgzuBSsoBglmgswNgm15kJOOAlZGn7AG84dw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+f1mzCITuFl3BXcq/x62wXcnsPYtLK8sHHqAsmh5zMOECgYBT+kIMHvmj1cBPztvb+cKBGTHRt8EDsxQr/v3Q6IabOgBNvC7FxSdwqsmJdKWbGN5gluRsYQ9dTjaFsqjWJToDcUDTPhoOf8DNKau9j+sDOVqVuf6BigXF/6NV1uSxayozuBACa5X2lbF7sLNQaeAnCjHhxNp46r5yaJzFqW0+8g==";


        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        //支付宝公钥
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjSdigNmFgIKlhWjFdwAzsez3Cos6lMvM+KmIRJCUhVBQsY8uufz0Ws8eeMVWH+t46OW27NFdSjr+ifw+iPNe3M6YImeORBYnO25Ks8qb7e0QPhnX0LYFaUV4lndpwB6UgSbJ1ySFRNIUJXUwBXtFEaIGK97pGBLaYmWm7ZujR4nzyeHUyZEfGelWrrf39QdgQNxCphiEdgDkf5Pgq/0rQkJC5sMPwmeFrcjig+DS5aqNVDMsnFHxsdrcM2FYQLFpDLlmWicV/dbqhuyNaQE5ROF//AFop0yT36UshCu1is4tD8ls9PdiOKrMONwWhxDwJFtdGE/aXOwUKXZlzSB6AwIDAQAB";

        //可设置异步通知接收服务地址（可选）
        config.notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址，例如：https://www.test.com/callback -->";

        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
        config.encryptKey = "<-- 请填写您的AES密钥，例如：aa4BtZ4tspm2wnXLb1ThQA== -->";

        return config;
    }
}
